package com.ykdl.member.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class DictionaryDataManager {
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_DATA = "data";
	public static final String KEY_SUB = "sub";
	public static final String TAG_ALL = "全部";
	public static final int DEFAULT_SALARY_CHECK_POSITION = 4; // 3000及以上。

	private static DictionaryDataManager mInstance;

	private DictionaryDataManager() {

	}

	public static DictionaryDataManager getInstance() {
		if (mInstance == null) {
			mInstance = new DictionaryDataManager();
		}
		return mInstance;
	}

	/**
	 * 获取字典数据，格式为json数组。
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 */
	public JSONArray getDataArray(Context context, String fileName) {
		JSONObject obj = DictionaryCache.getInstance().getDictDataByName(
				fileName);
		if (obj == null) {
			obj = DictionaryUtil.getInstance().readDictData(context, fileName);
		}
		return obj.getJSONArray(KEY_DATA);
	}

	/**
	 * 获取data的值是jsonobject格式。目前仅热门城市字典使用。
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 */
	public JSONObject getDataObject(Context context, String fileName) {
		JSONObject obj = DictionaryCache.getInstance().getDictDataByName(
				fileName);
		if (obj == null) {
			obj = DictionaryUtil.getInstance().readDictData(context, fileName);
		}
		return obj.getJSONObject(KEY_DATA);
	}

	/**
	 * 获取字典数据，支持JSON格式：["..",..]
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 */
	public List<String> getStringArray(Context context, String fileName) {
		JSONArray array = getDataArray(context, fileName);
		int size = array.size();
		List<String> values = new ArrayList<String>(size);
		for(int i = 0;i < size;i ++){
			values.add(array.getString(i));
		}
		return values;
	}

	/**
	 * 根据id查找名字
	 * 
	 * @param context
	 * @param fileName
	 * @param id
	 * @return
	 */
	public String getValueById(Context context, String fileName, long id) {
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return null;
		} else {
			return getValueById(array, id);
		}
	}

	/**
	 * 递归查找某个id的name值
	 * 
	 * @param array
	 * @param id
	 * @return
	 */
	private String getValueById(JSONArray array, long id) {
		Iterator<Object> it = array.iterator();
		while (it.hasNext()) {
			JSONObject obj = (JSONObject) it.next();
			if (obj.getLongValue(KEY_ID) == id) {
				return obj.getString(KEY_NAME);
			}
			if (obj.containsKey(KEY_SUB)) {
				JSONArray childArray = obj.getJSONArray(KEY_SUB);
				String value = getValueById(childArray, id);
				if (!TextUtils.isEmpty(value)) {
					return value;
				}
			}

		}
		return null;
	}

	/**
	 * 根据名称获取id
	 * 
	 * @param context
	 * @param fileName
	 * @param name
	 * @return
	 */
	public long getIdByName(Context context, String fileName, String name) {
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return -1;
		} else {
			JSONObject obj = getIdByName(array, name);
			if (obj != null) {
				return obj.getLongValue(KEY_ID);
			}
		}
		return -1;
	}

	/**
	 * 根据名字获取id
	 * 
	 * @param array
	 * @param name
	 * @return
	 */
	public JSONObject getIdByName(JSONArray array, String name) {
		Iterator<Object> it = array.iterator();
		while (it.hasNext()) {
			JSONObject obj = (JSONObject) it.next();
			if (name.equals(obj.getString(KEY_NAME))) {
				return obj;
			}
			if (obj.containsKey(KEY_SUB)) {
				JSONArray childArray = obj.getJSONArray(KEY_SUB);
				JSONObject object = getIdByName(childArray, name);
				if (object != null) {
					return object;
				}
			}

		}
		return null;
	}

	/**
	 * 获取一级数据
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 */
	public List<String> getParentDataList(Context context, String fileName) {
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return null;
		} else {
			List<String> parentDataList = new ArrayList<String>();
			Iterator<Object> it = array.iterator();
			while (it.hasNext()) {
				JSONObject obj = (JSONObject) it.next();
				parentDataList.add(obj.getString(KEY_NAME));
			}
			return parentDataList;
		}
	}

	/**
	 * 查找某个节点的所有子节点数据.
	 * 
	 * @param context
	 * @param fileName
	 * @param parentName
	 * @return
	 */
	public List<String> getChildDataList(Context context, String fileName,
			String parentName) {
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return null;
		} else {
			List<String> childDataList = new ArrayList<String>();
			JSONObject sub = getSubData(array, parentName);
			JSONArray childArray = sub.getJSONArray(KEY_SUB);
			Iterator<Object> it = childArray.iterator();
			while (it.hasNext()) {
				JSONObject obj = (JSONObject) it.next();
				childDataList.add(obj.getString(KEY_NAME));
			}
			return childDataList;
		}
	}

	/**
	 * 查找父节点
	 * 
	 * @param context
	 * @param fileName
	 * @param childName
	 * @return
	 */
	public JSONObject getParentData(Context context, String fileName,
			String childName) {
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return null;
		} else {
			Iterator<Object> pIt = array.iterator();
			while (pIt.hasNext()) {
				JSONObject object = (JSONObject) pIt.next();
				JSONArray childArray = object.getJSONArray(KEY_SUB);
				Iterator<Object> it = childArray.iterator();
				boolean isParent = false;
				while (it.hasNext()) {
					JSONObject obj = (JSONObject) it.next();
					if (childName.equals(obj.getString(KEY_NAME))) {
						isParent = true;
						break;
					}
				}
				if (isParent) {
					return object;
				}
			}

			return null;
		}
	}

	/**
	 * 递归查找父节点。
	 * 
	 * @param array
	 * @param parentName
	 * @return
	 */
	private JSONObject getSubData(JSONArray array, String parentName) {
		Iterator<Object> it = array.iterator();
		while (it.hasNext()) {
			JSONObject obj = (JSONObject) it.next();
			if (parentName.equals(obj.getString(KEY_NAME))) {
				return obj;
			}
			if (obj.containsKey(KEY_SUB)) {
				JSONArray childArray = obj.getJSONArray(KEY_SUB);
				getSubData(childArray, parentName);
			}

		}
		return null;
	}

	/**
	 * 选择器二级数据，key为对应的父数据，list为该父节点下的所有子节点。
	 * 
	 * @param context
	 * @param parentList
	 * @param fileName
	 * @return
	 */
	public Map<String, List<String>> getSubDataMap(Context context,
			List<String> parentList, String fileName) {
		Map<String, List<String>> subDataMap = new HashMap<String, List<String>>();
		for (String parent : parentList) {
			List<String> subList = getChildDataList(context, fileName, parent);
			subDataMap.put(parent, subList);
		}
		return subDataMap;
	}

	/**
	 * 根据内容和数据级别获取滚轮控件默认显示位置，暂时只支持2级。
	 * 
	 * @param context
	 * @param content
	 * @param fileName
	 * @param level
	 * @return
	 */
	public int[] getDefaultPosition(Context context, String content,
			String fileName, int level) {
		if (TextUtils.isEmpty(content)) {
			return new int[] { -1, -1 };
		}
		JSONArray array = getDataArray(context, fileName);
		if (array == null) {
			return null;
		} else {
			Iterator<Object> it = array.iterator();
			int i = 0;
			while (it.hasNext()) {
				JSONObject obj = (JSONObject) it.next();
				if (level == 1) {
					if (content.equals(obj.getString(KEY_NAME))) {
						return new int[] { i, -1 };
					}
				} else if (level == 2) {
					if (obj.containsKey(KEY_SUB)) {
						JSONArray childArray = obj.getJSONArray(KEY_SUB);
						Iterator<Object> childIt = childArray.iterator();
						int j = 0;
						while (childIt.hasNext()) {
							JSONObject object = (JSONObject) childIt.next();
							if (content.equals(object.getString(KEY_NAME))) {
								return new int[] { i, j };
							}
							j++;
						}
					}

				}
				i++;
			}
		}

		return new int[] { -1, -1 };
	}
}
